//
//  DetaisViewController.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DetaisViewController.h"

@interface DetaisViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, strong)UIView *headVIew;
@property(nonatomic, strong)NSMutableArray *arr; //总数组
@property(nonatomic, strong)UIView *footView;
@property(nonatomic, strong)UIImageView *bigImageView;
@property(nonatomic, strong)UILabel *readLabel;
@property(nonatomic, strong)MBProgressHUD *hud;
@property(nonatomic, strong)NSMutableArray *detaisArr;
@property(nonatomic, strong)baseDataModel *mod;
@end

@implementation DetaisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // tableView
    self.myTableView = [[UITableView alloc] init];
    [self.view addSubview:self.myTableView];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
    ////headView
    self.headVIew = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.myTableView.frame.size.width, 150)];
    self.headVIew.backgroundColor = [UIColor whiteColor];
    self.myTableView.tableHeaderView = self.headVIew;
    
    // 上面的图
    self.bigImageView = [[UIImageView alloc] init];
    [self.headVIew addSubview: self.bigImageView];
    [self.bigImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headVIew);
        make.right.equalTo(self.headVIew);
        make.top.equalTo(self.headVIew);
        make.height.mas_equalTo(60);
    }];

//
    // 上面的导读
    self.readLabel = [[UILabel alloc] init];
    [self.headVIew addSubview:self.readLabel];
    self.headVIew.backgroundColor = [UIColor clearColor];
    [self.readLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headVIew).with.offset(10);
        make.right.equalTo(self.headVIew).with.offset(-10);
        make.top.equalTo(self.bigImageView.mas_bottom);
        make.bottom.equalTo(self.headVIew);
    }];
    
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    
    if ([ZDQSingleton shareSingleton].isLongin == YES) {
        self.myTableView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
        self.readLabel.textColor = [UIColor whiteColor];
        self.headVIew.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
    } else {
        
        self.myTableView.backgroundColor = [UIColor whiteColor];
        self.readLabel.textColor = [UIColor blackColor];
        self.headVIew.backgroundColor = [UIColor whiteColor];
        
        
    }
    
    [self footButton];
}


//////下面的按钮
- (void)footButton {
       /// 收藏按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang.png"] style:UIBarButtonItemStyleDone target:self action:@selector(clakc:)];
    
    [self createData];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"正在加载";
}

#pragma mark 网络请求
- (void)createData {
    
    [SLAFNetWorkTool cacheGetWithURL:self.strId cookie:nil success:^(id responseObject) {
        self.arr = [NSMutableArray array];
        NSMutableArray *bigArr = [NSMutableArray array];
        self.mod = [[baseDataModel alloc] init];
        self.mod.tempId = self.strId;
        for (NSDictionary *dic in responseObject[@"body"][@"subjects"]) {
            
            if ([dic[@"view"] isEqualToString:@"multiTitle"]) {
                [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:dic[@"content"][@"bgImage"]]];
                self.mod.image = dic[@"content"][@"bgImage"];
            }
            else if ([dic[@"view"] isEqualToString:@"text"]) {
                self.readLabel.text = dic[@"content"][@"intro"];
                self.readLabel.numberOfLines = 0;
                self.readLabel.textColor = [UIColor grayColor];
                self.readLabel.font = [UIFont systemFontOfSize:14];
                [self.readLabel sizeToFit];
                self.mod.title = dic[@"content"][@"intro"];
            }
            else if ([dic[@"view"] isEqualToString:@"list"]) {
                        [bigArr addObject:dic];
            }
        }
        
        // 数据库
        [DataBaseHandle shareDataBasaHandle];
        [[DataBaseHandle shareDataBasaHandle] createTable];
        
        //////判断是否有无
        SelectbaseDataModel result = [[DataBaseHandle shareDataBasaHandle] isHaveInTable:self.mod];
        if (result == Intable) {
            /// 收藏按钮
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yishoucang.png"] style:0 target:self action:@selector(clakc:)];
        } else if (result == NotInTable) {
            /// 收藏按钮
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang.png"] style:0 target:self action:@selector(clakc:)];
        } else {
            NSLog(@"出错");
        }

        //// 分区数组
        self.arr = [DetaisModel baseModelByArr:bigArr];
//        NSLog(@"...%ld", self.arr.count);
        ///每个分区详情Cell
        self.detaisArr = [NSMutableArray array];
        for (DetaisModel *deta in self.arr) {
            NSMutableArray *tempArr = [DetaisModel baseModelByArr:deta.podItems];
            [self.detaisArr addObject:tempArr];
        }
        
//        NSLog(@"%@", self.detaisArr);
        [self.myTableView reloadData];
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
    }];
}
#pragma mark 分区
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

// 分区数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arr.count;
}

// 分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    DetaisModel *deais = self.arr[section];
    return deais.title;
}
// 每个分区中cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    DetaisModel *deat = self.arr[section];
    return deat.podItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *modArr = self.detaisArr[indexPath.section];
    DetaisModel *detais = modArr[indexPath.row];
    if (indexPath.section < self.detaisArr.count - 1) {
        static NSString *reuse = @"reuse";
        TopicPageCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[TopicPageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        
        cell.backgroundColor = [UIColor clearColor];
        [cell.picImage sd_setImageWithURL:[NSURL URLWithString:detais.thumbnail] placeholderImage:[UIImage imageNamed:@"pac.png"]];;
        cell.titleLabel.text = detais.title;
        
        if ([ZDQSingleton shareSingleton].isLongin == YES) {
            cell.picImage.alpha = 0.4;
            cell.titleLabel.textColor = [UIColor whiteColor];
        } else {
            cell.picImage.alpha = 1;
            cell.titleLabel.textColor = [UIColor blackColor];
        
        }
        
        return cell;
    }
    else {
        static NSString *reuse = @"ram";
         DetaisViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[DetaisViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        cell.backgroundColor = [UIColor clearColor];
        cell.titleLabel.text = detais.title;
        cell.talkLabel.text = [NSString stringWithFormat:@"%@ 阅", detais.commentCount];
        cell.titleLabel.numberOfLines = 0;
        [cell.titleLabel sizeToFit];
        
        if ([ZDQSingleton shareSingleton].isLongin == YES) {
            cell.titleLabel.textColor = [UIColor whiteColor];
            cell.talkLabel.textColor = [UIColor whiteColor];
        } else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.talkLabel.textColor = [UIColor blackColor];
            
        }
        
        return cell;
    }
}
#pragma mark 点击方法
// 点击方法 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *arr = self.detaisArr[indexPath.section];
    DetaisModel *detais = arr[indexPath.row];
    DetaisWebViewController *webVC = [[DetaisWebViewController alloc] init];
    webVC.strId = detais.shareUrl;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)clakc:(UIBarButtonItem *)button {
    if ([button.image isEqual:[UIImage imageNamed:@"shoucang.png"]]) {
        [[DataBaseHandle shareDataBasaHandle] saveNewsInDB:self.mod];
        button.image = [UIImage imageNamed:@"yishoucang.png"];
    }
    else {
        [[DataBaseHandle shareDataBasaHandle] deleteNewsInDB:self.mod];
        button.image = [UIImage imageNamed:@"shoucang.png"];
    }

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
